package com.zx.idc.backend.gui.api.watch.handler;

import com.zx.idc.backend.gui.api.ftp.Stat;
import com.zx.idc.backend.gui.api.watch.handler.base.AbstractFtpUploadResultHandler;
import com.zx.idc.backend.gui.api.watch.pojo.FtpUploadResult;
import com.zx.idc.cache.ins.InsLogCountCacheService;
import com.zx.idc.common.constants.InstructionType;
import com.zx.idc.common.lang.ListUtil;
import com.zx.idc.ds.common.constant.ReportStatusConstant;
import com.zx.idc.ds.ins.entity.InsFilter;
import com.zx.idc.ds.ins.service.IInsFilterService;
import com.zx.idc.xml.entity.common.filtermonitor.log.Log;
import com.zx.idc.xml.entity.filterresult.FilterResult;
import com.zx.idc.xml.utils.JaxbUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import static com.zx.idc.common.attributes.FTPReportDataTypeAttributes.FTP_ILLEGAL_INFORMATION_FILTERING_RECORD;

/**
 * 违法信息过滤记录结果处理器
 */
@Component
//@Transactional(rollbackFor = Exception.class) //不需要声明事务,当前的类相当于controller
public class IllegalInformationFilterRecordResultHandler extends AbstractFtpUploadResultHandler {


    @Autowired
    private IInsFilterService iInsFilterService;

    @Autowired
    private InsLogCountCacheService insLogCountCacheService;

    /**
     * 匹配
     *
     * @param type
     * @return
     */
    @Override
    public boolean adapter(String type) {
        return FTP_ILLEGAL_INFORMATION_FILTERING_RECORD.equals(type);
    }

    /**
     * 上报成功回调,需要处理结果则重写改方法
     *
     * @param stat            ftp扫描的原信息对象
     * @param ftpUploadResult 解析后的ftp上报结果对象
     * @param filePath        上报源文件的全路径
     */
    @Override
    public void uploadSuccess(Stat stat, FtpUploadResult ftpUploadResult, String filePath) {
        log.debug("Illegal information filtering record result success processor start!");
        updateReportStatus(filePath, ReportStatusConstant.REPORTED);
        log.debug("Illegal information filtering record result success processor done!");
    }

    /**
     * 上报异常回调,需要处理结果则重写改方法
     *
     * @param stat            ftp扫描的原信息对象
     * @param ftpUploadResult 解析后的ftp上报结果对象
     * @param filePath        上报源文件的全路径
     */
    @Override
    public void uploadFailed(Stat stat, FtpUploadResult ftpUploadResult, String filePath) {
        log.debug("Illegal information filtering record result failed processor start!");
        updateReportStatus(filePath, ReportStatusConstant.REPORTING_EXCEPTION);
        log.debug("Illegal information filtering record result failed processor done!");
    }

    /**
     * 更新上报状态模板
     *
     * @param filePath     源文件全路径
     * @param reportStatus
     */
    private void updateReportStatus(String filePath, Integer reportStatus) {
        FilterResult filterResult = JaxbUtils.xmlFileToBean(filePath, FilterResult.class);
        List<Log> logs = filterResult.getLog();
        if (ListUtil.isEmpty(logs)) {
            return;
        }

        Map<Long, List<Log>> groupMap = logs.stream().collect(Collectors.groupingBy(Log::getCommandId));
        List<InsFilter> updateInsList = groupMap.keySet().stream().map(commandId -> {
            InsFilter filter = new InsFilter();
            filter.setId(commandId+"");
            filter.setReportStatus(reportStatus);
            return filter;
        }).collect(Collectors.toList());

        iInsFilterService.updateBatchById(updateInsList);

        if (!reportStatus.equals(ReportStatusConstant.REPORTED)) {
            return;
        }
        //统计已上报记录
        groupMap.forEach((insId, dataList) ->
            insLogCountCacheService.increaseUploadCount(
                InstructionType.INS_FILTER,
                insId+"",
                dataList.size()
            )
        );

    }
}
